import request from '@/utils/request';
import { TableListParams, Category } from './data';
import client from '@/client';

export async function queryRule(params?: Object) {
  return request('/api/goods/category/list', {
    method: 'GET',
    params,
  });
}


export async function queryAllRule(params?: Object) {
  return request('/api/goods/category/all', {
    method: 'GET',
    params,
  });
}

export async function removeRule(data: { id: number }) {
  return request(`/api/goods/category/${data.id.toString()}`, {
    method: 'DELETE',
    // data,
  });
}

export async function addRule(data: object) {
  return request('/api/goods/category', {
    method: 'POST',
    data,
  });
}

export async function updateRule(id: number, data: TableListParams) {
  return request(`/api/goods/category/${id}`, {
    method: 'PUT',
    data,
  });
}



export async function queryCategory(){
   const queryStruct = { 
     query: `{
          category{
            id
            name
            desc
            pid
          }
        }`
    }

    const { data } = await client.query<{ category: Category[] }>(queryStruct)
    return data
}

export async function addCategory(category: Category){
  
  const { data } = await client.mutate<Category>({
    mutation: `mutation { 
      createCategory(name: "${category.name}", desc:"${category.desc}", pid: ${category.pid || null}){
      id,
      name
    }
    }`
  })

  return data
}

export async function updateCategory(category: Category){
  
  const { data } = await client.mutate<Category>({
    mutation: `mutation { 
      updateCategory(id: ${category.id}, name: "${category.name}", desc:"${category.desc}", pid: ${category.pid || null}){
      id,
      name
    }
    }`
  })

  return data
}

export async function delCategory(id: number){
  return await client.mutate({
    mutation: `
    mutation{
      deleteCategory(id: ${id})
    }
    `
  })
}